﻿using System;
using System.Windows;
using Phoenix.Frames.Transitions;
using Transitionals.Controls;

namespace Phoenix.Frames
{
    [TemplatePart(Name = "TransitionElement", Type = typeof(TransitionPartialView))]
    public class TransitionPartialView : PartialView
    {
        TransitionElement transitionElement;
        const string TransitionElementPartName = "TransitionElement";

        static TransitionPartialView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TransitionPartialView), new FrameworkPropertyMetadata(typeof(TransitionPartialView)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            transitionElement =
                GetTemplateChild(TransitionElementPartName) as TransitionElement ?? 
                Template.FindName(TransitionElementPartName, this) as TransitionElement;
            if (Content != null)
            {
                OnContentChanged(null, Content);
            }
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            TransitionService.TransitionContent(oldContent, newContent, transitionElement, true);
        }
    }
}